package com.greenantmall.view;

import java.util.ArrayList;

import com.greenantmall.R;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SingleChooseDialog extends Dialog implements OnClickListener,
    OnKeyListener
{
    // CheckBox mCheck;
    AutoCompleteTextView mAutoComplete;
    Context context;
    TextView mTitle;
    boolean isCheck = true;
    private ListView listView;
    private OnItemClickListener listener;
    private ArrayList<String> itemList;
    
    
    public SingleChooseDialog(Context context,String title)
    {
        super(context, R.style.ExitDialogStyle);
        init(context,title);
    }

    public void init(Context context,String title)
    {
        setDialogTitle();
        this.context = context;
        this.setCanceledOnTouchOutside(false);
        mTitle = (TextView)findViewById(R.id.commondialog_title);
        mTitle.setText(title);
        itemList = new ArrayList<String>();
        
        listView = (ListView)findViewById(R.id.listView);
        listView.setAdapter(new PopAdapter());
        listView.setFocusableInTouchMode(true);
        listView.setFocusable(true);
        
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }


    void setDialogTitle()
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        this.setContentView(R.layout.singlechoose);
    }

    // 想要退出时聚焦到取消按钮上
    // by dong 2012-1-6
    @Override
    public void show()
    {
        super.show();
        // 默认取消按钮获得焦点
    }
    public void addItems(String[] items) {
        for (String s : items)
            itemList.add(s);
      
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
        listView.setOnItemClickListener(listener);
    }
    
    // ������
    private final class PopAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return itemList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return itemList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.singlechoose_item, null);
                holder = new ViewHolder();
                holder.groupItem = (TextView) convertView.findViewById(R.id.textView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.groupItem.setText(itemList.get(position));
            convertView.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    if(listener!=null)
                    {
                        listener.onItemClick(null, null, position, position);
                    }
                    //popupWindow.dismiss();
                    dismiss();
                }
            });
            holder.groupItem.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    if(listener!=null)
                    {
                        listener.onItemClick(null, null, position, position);
                    }
                   // popupWindow.dismiss();
                    dismiss();
                }
            });
             
        
            return convertView;
        }

        private final class ViewHolder {
            TextView groupItem;
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        return false;
    }

    @Override
    public void onClick(View v)
    {
        
    }

}
